#!/bin/bash

mkdir -p `dirname $0`/logs
LOG=`dirname $0`/logs/init.ssh.log
date > $LOG
cd $HOME

SSH_KEYGEN1=
SSH_KEYGEN2=

########### configure ###########

ssh-keygen -V &>/dev/null
exit_code=$?
if [ "$exit_code" -eq 127 ]; then
    echo "not find command ssh-keygen"
else
    if [ "$exit_code" -ne 0 ]; then
        SSH_KEYGEN1=`which ssh-keygen`    
    else
	SSH_KEYGEN2=`which ssh-keygen`
    fi
fi

ssh-keygen2 -V &>/dev/null
exit_code=$?
if [ "$exit_code" -eq 127 ]; then
    echo "not find command ssh-keygen2"
else
    if [ "$exit_code" -ne 0 ]; then
        SSH_KEYGEN1=`which ssh-keygen2`    
    else
	SSH_KEYGEN2=`which ssh-keygen2`
    fi
fi

########### generate keys ###########

if [ ! -z "$SSH_KEYGEN1" ]; then
    echo "ssh keys generate by $SSH_KEYGEN1 ..."
    mkdir -p .ssh
    rm -f .ssh/id_dsa
    $SSH_KEYGEN1 -t dsa -N "" -f .ssh/id_dsa >>$LOG
    $SSH_KEYGEN1 -x -f .ssh/id_dsa > .ssh/id_dsa_ssh2.pub	
else
    echo "warn: not find command for generate ssh keys"
fi    

if [ ! -z "$SSH_KEYGEN2" ]; then
    echo "ssh2 keys generate by $SSH_KEYGEN2 ..."
    mkdir -p .ssh2
    $SSH_KEYGEN2 -b 1024 -t dsa -P >>$LOG
    mv .ssh2/id_dsa_1024_a .ssh2/id_dsa
    mv .ssh2/id_dsa_1024_a.pub .ssh2/id_dsa.pub
    [ ! -z "$SSH_KEYGEN1" ] && $SSH_KEYGEN1 -X -f .ssh2/id_dsa.pub > .ssh2/id_dsa_ssh.pub 2>>$LOG
    echo "IdKey id_dsa" > .ssh2/identification
else
    echo "warn: not find command for generate ssh2 keys"
fi    

